import { onMounted, onUnmounted, ref } from "vue"
import {throttle} from 'underscore';

export default function () {
    // 首页列表滚动条监听函数
    const scrollOnBottom = ref(false);
    const nowScroll = ref(0);
    const sumScroll = ref(0);
    const docuScroll = ref(0)
    // 节流函数，设置0.1秒内只触发一次
    const scrollAjax =  throttle(function(){
            // 当前滚动轴高度
            nowScroll.value = this.document.documentElement.scrollTop
            // 总的滚动轴高度 = 滚动轴 + DOM高度
            sumScroll.value = this.document.documentElement.scrollHeight
            // 当前显示DOM内容的高度
            docuScroll.value = this.document.documentElement.clientHeight
            if (docuScroll.value + nowScroll.value+1 >= sumScroll.value) {
                scrollOnBottom.value = true;
            }
    },100)
    onMounted(() => {
        window.addEventListener('scroll', scrollAjax)
    })
    onUnmounted(() => {
        window.removeEventListener('scroll', scrollAjax)
    })
    return {
        scrollOnBottom,
        nowScroll,
        sumScroll,
        docuScroll
    }
}